! function(global, factory) {
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global.imconfig_touch = factory());
}(this, function() {
  function myTouch(el) {
    if(typeof el !== 'object' || el.nodeType !== 1){
      throw new Error('this touch lib need a right el.');
    }
    el.addEventListener('touchstart', touchStart, false);
    el.addEventListener('touchmove', touchMove, false);
    el.addEventListener('touchend', touchEnd, false);

    el.is_imconfig_touch = true;

    this.el = el;
    this.touch = false;
    this.time = 0;
    this.type = false;
    this.poss = false;
    this.x = 0;
    this.y = 0;
    this.z = 0;
    this.r = 0;
    this.s = 1;

    this.tx = 0;
    this.ty = 0;
    this.tz = 0;
    this.tr = 0;
    this.ts = 1;
    this.begin = [];
    var touch = this;

    function touchStart(e) {
      e.preventDefault();
      touch.touch = true;
      var poss = e.touches;
      if (poss.length === 1) {
        touch.begin[0] = {
          clientX: poss[0].clientX,
          clientY: poss[0].clientY
        };
      } else if (poss.length === 2) {
        touch.begin[0] = {
          clientX: poss[0].clientX,
          clientY: poss[0].clientY
        };
        touch.begin[1] = {
          clientX: poss[1].clientX,
          clientY: poss[1].clientY
        };
      }
    }

    function touchMove(e) {
      var poss = e.touches;
      e.preventDefault();
      if (touch.touch !== true) {
        return;
      }
      if (poss.length === 1) {
        var posB = touch.begin[0];
        var posE = poss[0];
        touch.tx = posE.clientX - posB.clientX;
        touch.ty = posE.clientY - posB.clientY;
      } else if (poss.length === 2) {
        var b1 = touch.begin[0];
        var b2 = touch.begin[1];
        var n1 = poss[0];
        var n2 = poss[1];
        var deg1 = touch.getDeg(b1, b2);
        var deg2 = touch.getDeg(n1, n2);
        touch.tr = deg2 - deg1;
        var lin1 = touch.getLineWidth(b1, b2);
        var lin2 = touch.getLineWidth(n1, n2);
        touch.ts = lin2 / lin1;
      }
      touch.refresh();
    }

    function touchEnd(e) {
      var poss = e.touches;
      e.preventDefault();
      if (touch.touch !== true) {
        return;
      }

      touch.touch = false;
      touch.refresh('end');
    }
  }

  myTouch.prototype.getLineWidth = function(b, e) {
    var dx = e.clientX - b.clientX;
    var dy = e.clientY - b.clientY;
    return Math.sqrt(dx*dx + dy*dy);
  };
  myTouch.prototype.getDeg = function(b, e) {
    var dx = e.clientX - b.clientX;
    var dy = e.clientY - b.clientY;
    var tan = dy / dx;
    return Math.atan(tan) / Math.PI * 180;
  };
  myTouch.prototype.refresh = function(type) {
    if (type === 'end') {
      this.x += this.tx;
      this.y += this.ty;
      this.z += this.tz;
      this.r = (this.r + this.tr) % 360;
      this.s *= this.ts;

      this.tr = 0;
      this.tx = 0;
      this.ty = 0;
      this.tz = 0;
      this.ts = 1;
    }
    var style = [];
    style.push(['translate3d(', this.x + this.tx, 'px, ', this.y + this.ty, 'px, ', this.z + this.tz, 'px)'].join(''));
    style.push(['rotateZ(', this.r + this.tr, 'deg)'].join(''));
    style.push(['scale(' + this.s * this.ts + ')'].join(''));
    this.el.style.transform = style.join(' ');
  };

  return myTouch;
});